package jym.tan.movielibrary.url;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class URLHandler {

	private static class LazyURLHandler {
		private static final URLHandler URL_HANDLER;

		static {
			URL_HANDLER = new URLHandler();
		}
	}

	public static URLHandler getInstance() {
		return URLHandler.LazyURLHandler.URL_HANDLER;
	}

	public String getJsonStringFromURL(String urlString) throws IOException {		
		BufferedReader reader = new BufferedReader(new InputStreamReader(((HttpURLConnection) (new URL(urlString)).openConnection()).getInputStream(), Charset.forName("UTF-8")));
		StringBuilder jsonString = new StringBuilder();;
		String line;
		while ((line = reader.readLine()) != null) {
			jsonString.append(line);
		}			
		reader.close();		
		return jsonString.toString();
	}
}
